/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package utility.com;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.text.NumberFormat;
import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

/**
 *
 * @author jay
 */
public class TableUtil {
    public TableUtil(){}
    
    public static void setTableActionKeyMap(JTable table) {

        table.getInputMap(JTable.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT).put(KeyStroke.getKeyStroke("ENTER"), "NEXT_EDITABLE_CELL_ENTER");
        table.getActionMap().put("NEXT_EDITABLE_CELL_ENTER", new AbstractAction() {
            
            @Override
            public void actionPerformed(ActionEvent e) {
                JTable t = (JTable) e.getSource();
                int row, column;
                if (t.isEditing()) {
                    row = t.getEditingRow();
                    column = t.getEditingColumn();
                } else {
                    row = t.getSelectionModel().getAnchorSelectionIndex();
                    if (row == -1) {if (t.getRowCount() == 0) {return;}}
                    
                    column = t.getColumnModel().getSelectionModel().getAnchorSelectionIndex();
                    if (column == -1) {
                        if (t.getColumnCount() == 0) {return;}
                        column = 0;
                    }
                }

                column++;
                int rows = t.getRowCount();
                int columns = t.getColumnCount();
                
                while (row < rows) {
                    while (column < columns) {
                        if (t.isCellEditable(row, column)) {
                            t.changeSelection(row, column, true, true);
                            t.setRowSelectionInterval(row, row);
                            t.setColumnSelectionInterval(column, column);
                            t.editCellAt(row, column);
                           
                            if ((t.getEditingRow() == row) && (t.getEditingColumn() == column)) {

                                Component cell = t.getEditorComponent();
                                if(cell instanceof JTextField) {
                                    ((JTextField)cell).selectAll();
                                } else if(cell instanceof JComboBox) {
                                    t.getCellEditor().cancelCellEditing();
                                    ((JComboBox)cell).setRequestFocusEnabled(false);
                                }
                                return;
                           }
                           return;
                        }
                        column++;
                    }
                    column= 0;
                    row++;
                }
            }
        });
    }
    
    public class NumberRenderer extends DefaultTableCellRenderer{

        public NumberRenderer() {
            super();
            setHorizontalAlignment(SwingConstants.RIGHT);
        }

        @Override
        public void setValue(Object value) {

            if ((value != null) && (value instanceof Number)) {
                Number numberValue = (Number) value;
                NumberFormat formatter = NumberFormat.getIntegerInstance();
                value = formatter.format(numberValue.intValue());
            }
            super.setValue(value);
        }
    }   

    public class CurrencyRenderer extends DefaultTableCellRenderer{
        public CurrencyRenderer() {
            super();
            setHorizontalAlignment(SwingConstants.RIGHT);
        }
        
        @Override
        public void setValue(Object value) {

            if ((value != null) & (value instanceof Number)) {

                Number numberValue = (Number) value;
                NumberFormat formatter = NumberFormat.getNumberInstance();
                value = formatter.format(numberValue.doubleValue());
            }        
            super.setValue(value);
        }
    }
    
    public static void removeOneRow(JTable jtable) {
        int rowCount = jtable.getRowCount();
        int row = jtable.getSelectedRow();
        if (rowCount > 1 && row >-1 ) {
            ((DefaultTableModel)jtable.getModel()).removeRow(row);
        } else if(rowCount == 1 && row >-1) {
            for (int i=0; i<jtable.getColumnCount();i++) {
                jtable.setValueAt("", 0, i);
            }
        }
    
    }
    
}
